const path=require('path');

const webpackBase = require("./webpack.config.base");

const webpackMerge = require("webpack-merge");

const webpack = require("webpack");

const ExtractTextPlugin = require("extract-text-webpack-plugin");

const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');

const CompressionWebpackPlugin=require('compression-webpack-plugin');

const config = require('./index');

module.exports = webpackMerge(webpackBase,{
    module:{
        rules:[
            {

                test: /\.(less|css)$/,
                exclude: /node_modules/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    publicPath: config.cssPublicPath,
                    use: [{
                            loader:"css-loader",
                        },
                        {
                            loader:"postcss-loader",
                             options: {
                                plugins: (loader) => [
                                    require('autoprefixer')()
                                ]
                            }                           
                        },
                        {
                          loader:'less-loader'
                        }
                    ]
                })
            },              
        ]
    },   
    plugins:[
        new ExtractTextPlugin(config.cssOutputPath),  
        new OptimizeCSSPlugin({
          cssProcessorOptions:{ 
            safe: true 
         }
        }),        
    new webpack.optimize.UglifyJsPlugin({ 
            beautify: false,
            comments: false,
            compress: {
                warnings: false,
                drop_console: true,
                collapse_vars: true,
                reduce_vars: true,
            },
            mangle:{
                except:['exports','require']
            }            
        }),
        new webpack.optimize.ModuleConcatenationPlugin(), 
        new webpack.optimize.OccurrenceOrderPlugin(true),  
        new webpack.optimize.CommonsChunkPlugin({
          name: 'vendor',
          minChunks: function (module) {
            return (
              module.resource &&
              /\.js$/.test(module.resource) &&
              module.resource.indexOf(
                path.join(__dirname, './node_modules')
              ) === 0
            )
          }
        }), 
        new CompressionWebpackPlugin({
          asset: '[path].gz[query]',
          algorithm: 'gzip',
          test: new RegExp(
                '\\.(js|css)$'
          ),
          threshold: 10240,
          minRatio: 0.8
        })                          
    ]
});
